from django.shortcuts import render,HttpResponse,redirect,reverse,resolve_url
import chunk

# Create your views here.
def zzb(request):
    return redirect('shang')
def zzc(requst):
    return render(requst,'index.html',context={'list':['赵大','钱二','孙三','李四','周武','吴六','赵','钱','孙','李','周','武','郑','王','张','贺',]})
def zbc(request):
    return render(request,'index1.html',context={'human':{'name':'张三','age':18,'hobby':'backball','sex':'未知u'}})
def shou(request):
    idcard=request.GET.get('idcard')
    name=request.GET.get('name')
    return HttpResponse(f'身份证:{idcard},姓名:{name}')
def shou1(request,name,idcad):

    return HttpResponse(f"姓名:{name},Bala:{idcad}")
def shou2(request,name,idcab):
    return HttpResponse(f'name:{name},pipi:{idcab}')
def language(request):
    ie=request.GET.get('ie')
    wd=request.GET.get('wd')
    return HttpResponse(f'ie:{ie},wd:{wd}')
def mkform(request):
    id=request.GET.get('id')
    name=request.GET.get('name')
    return render(request,'add.html')
def mkform2(request):
    return HttpResponse('添加成功')
def request1(request):
    if request.method=="GET":
        return render(request,'request.html')
    elif request.method=='POST':
        name=request.POST.get('name')
        sex=request.POST.get('sex')
        return HttpResponse(f'姓名:{name},性别:{sex}')

# 文件下载
def downfile(request):
    if request.method=='GET':
        return render(request,'downfile.html')
    elif request.method=="POST":
        file=request.FILES.get('file')
        print(type(file))
        with open('joker.jpg','wb')as a:
            for i in file.chunks():
                a.write(i)
        return HttpResponse('下载完成')



def shang(request):
    # print(request.get_raw_uri())
    # host=request.get_host()
    # print(host)
    if request.method=='GET':
        return render(request,'sh.html')
    elif request.method=="POST":

        if 'HTTP_X_FORWARDER' in request.META:
            user_ip=request.META['HTTP_X_FORWARDER']
        else:
            user_ip=request.META['REMOTE_ADDR']
        name=request.POST.get('name')
        age=request.POST.get('age')
        iphon=request.POST.get('iphon')
        picture=request.FILES.get('picture')
        with open('jok.jpg','wb')as af:
            for i in picture.chunks():
                af.write(i)
        return HttpResponse(f'姓名:{name},年龄:{age},电话:{iphon},头像照片{picture},ip地址:{user_ip}')
#  反向路由解析
def abc(request):
    p=reverse('shou1',args=['张三',5])
    p = reverse('shou1', kwargs={'name': '航三', 'idcad': 15})
    p=resolve_url('shou1','狗蛋',15)
    print(p)
    return redirect(p)
# 过滤器
def filter(request):
    return render(request,'filter.html',{'name':'jack'})
#继承的操作方法
def inherit(request):
    return render(request,'inherit.html')
# 反向解析
def reverses(request,name,age):
    return HttpResponse(f'姓名：{name},年龄：{age}')
def dnfff(request):
    return render(request,'dnf.html')

